package io.jenkins.plugins.feiqiu;

import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;
import hudson.tasks.Recorder;
import io.jenkins.plugins.feiqiu.FeiqUtil.SendRangeOpt;
import java.io.File;
import java.io.IOException;
import java.util.Objects;
import org.apache.commons.io.FileUtils;
import org.kohsuke.stapler.DataBoundConstructor;

public class FeiqiuNotify extends Recorder {

  private String mac;
  private String userName;
  private String pcName;
  //  private final TaskProperties properties;
//  private FeiqiuTarget[] targets;
  private String text;
  private String textFile;
  private String ipRange;
  private Integer ipStart;
  private Integer ipEnd;

  private Long groupId;

  @DataBoundConstructor
  public FeiqiuNotify(String text, String textFile, String userName,
      String pcName, String mac, String ipRange, Long groupId,
      Integer ipStart, Integer ipEnd) {
    this.groupId = groupId;
    this.ipRange = ipRange;
    this.ipStart = ipStart;
    this.ipEnd = ipEnd;
    this.text = text;
    this.textFile = textFile;
    this.userName = userName;
    this.pcName = pcName;
    this.mac = mac;
  }

  public Long getGroupId() {
    return groupId;
  }

  public void setGroupId(Long groupId) {
    this.groupId = groupId;
  }

  public String getIpRange() {
    return ipRange;
  }

  public void setIpRange(String ipRange) {
    this.ipRange = ipRange;
  }

  public Integer getIpStart() {
    return ipStart;
  }

  public void setIpStart(Integer ipStart) {
    this.ipStart = ipStart;
  }

  public Integer getIpEnd() {
    return ipEnd;
  }

  public void setIpEnd(Integer ipEnd) {
    this.ipEnd = ipEnd;
  }

  public String getMac() {
    return mac;
  }

  public void setMac(String mac) {
    this.mac = mac;
  }

  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public String getPcName() {
    return pcName;
  }

  public void setPcName(String pcName) {
    this.pcName = pcName;
  }
//
//  public FeiqiuTarget[] getTargets() {
//    return targets;
//  }
//
//  public void setTargets(FeiqiuTarget[] targets) {
//    this.targets = targets;
//  }

  public String getText() {
    return text;
  }

  public void setText(String text) {
    this.text = text;
  }

  public String getTextFile() {
    return textFile;
  }

  public void setTextFile(String textFile) {
    this.textFile = textFile;
  }

  @Override
  public BuildStepMonitor getRequiredMonitorService() {
    return BuildStepMonitor.NONE;
  }

  @Override
  public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
      throws InterruptedException, IOException {
    listener.getLogger().println("Feiqiu Task start...");
    String msg = this.text;
    if (Objects.isNull(msg) || msg.isEmpty()) {
      msg = FileUtils.readFileToString(new File(this.textFile), "GBK");
    }
    listener.getLogger().println("msg is:" + msg);
//      listener.getLogger().println("send msg to:" + target.toString());
    SendRangeOpt opt = new SendRangeOpt();
    opt.ipRange = this.getIpRange();
    opt.start = this.getIpStart();
    opt.end = this.getIpEnd();
    opt.speed = 100l;
    try {
//        if (target.getGroupMsg()) {
//          FeiqUtil.sendGroupMsg(msg, this.userName, this.pcName, "FAFFC2102064", target);
//        } else {
      FeiqUtil.sendGroupMsg(this.groupId, msg, this.userName, this.pcName, this.mac, opt,
          listener.getLogger());
//        }
    } catch (Exception e) {
      e.printStackTrace();
//        throw new RuntimeException(e);
    }
//    }
    listener.getLogger().println("Feiqiu Task success");
    return true;
  }

  @Extension
  public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> {

    public DescriptorImpl() {
      super(FeiqiuNotify.class);
      load();
    }

    @Override
    public String getDisplayName() {
      return "Feiqiu notify task";
    }

//    @Override
//    public FeiqiuNotify newInstance(@Nullable StaplerRequest req, @NonNull JSONObject formData)
//        throws FormException {
//      FeiqiuNotify f = req.bindJSON(FeiqiuNotify.class, formData);
//      return f;
//    }

    @Override
    public boolean isApplicable(Class<? extends AbstractProject> jobType) {
      return true;
    }
  }
}
